﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using PagedList.Mvc;
using PagedList;
using WineStore.Models;
using System.Data;
using System.IO;
using System.Data.Entity;

namespace WineStore.Controllers
{
    public class LogoController : Controller
    {
        //
        // GET: /Logo/

        public ViewResult ShowLogo()
        {
            using (var db = new WineDbContext())
            {
                var logo = db.Logo.ToList();
                return View(logo);
            }
        }

        // GET: /Logo/CreateLogo/
        [HttpGet]
        public ActionResult CreateLogo()
        {

            return View();
        }

        // POST: /Logo/CreateLogo/
        [HttpPost]
        public ActionResult CreateLogo(Logo logo, HttpPostedFileBase LogoImage)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Luu anh len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Home"), System.IO.Path.GetFileName(LogoImage.FileName));
                    LogoImage.SaveAs(path);
                    // Luu anh vao database 
                    logo.LogoImage = LogoImage.FileName;
                    using (var db = new WineDbContext())
                    {
                        db.Entry(logo).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowLogo", "Logo");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }

            return View(logo);
        }


        // GET: /Logo/EditLogo/
        [HttpGet]
        public ActionResult EditLogo(int id = 0)
        {
            Logo logo = null;
            using (var db = new WineDbContext())
            {
                logo = db.Logo.Find(id);
                if (logo == null)
                {
                    return HttpNotFound();
                }
            }

            return View(logo);
        }

        // POST: /Logo/EditLogo/
        [HttpPost]
        public ActionResult EditLogo(Logo logo, HttpPostedFileBase LogoImage)
        {
            try
            {
                // Lay ve duong dan anh cu
                logo.LogoImage = (new WineDbContext().Logo.Find(logo.LogoId)).LogoImage;
                //if (ModelState.IsValid)
                //{
                // Neu update anh moi thi thuc hien khoi lenh trong than cau lenh if
                if (LogoImage != null)
                {
                    // Xoa anh cu tren server
                    FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/Home/" + logo.LogoImage));
                    if (fileInfo != null)
                    {
                        fileInfo.Delete();
                    }
                    // Luu anh moi len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Home"), System.IO.Path.GetFileName(LogoImage.FileName));
                    LogoImage.SaveAs(path);
                    // Luu duong dan anh vao database 
                    logo.LogoImage = LogoImage.FileName;
                }

                using (var db = new WineDbContext())
                {
                    db.Entry(logo).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowLogo", "Logo");
                }
                //}
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }

            return View(logo);
        }

        // GET: /Logo/DeleteLogo/
        public ActionResult DeleteLogo(int id)
        {
            Logo logo = null;
            using (var db = new WineDbContext())
            {
                logo = db.Logo.Find(id);
                if (logo == null)
                {
                    return HttpNotFound();
                }
            }
            return View(logo);
        }

        // POST: /Logo/DeleteLogo/
        [HttpPost, ActionName("DeleteLogo")]
        public ActionResult DeleteConfirmed(int id)
        {
            Logo logo = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    logo = db.Logo.Find(id);
                    // Xoa anh tren server
                    FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/Home/" + logo.LogoImage));
                    if (fileInfo != null)
                    {
                        fileInfo.Delete();
                    }
                    db.Entry(logo).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowLogo", "Logo");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(logo);
        }
    }
}
